package org.playground.booking.security.auth.Impl;

import org.playground.booking.auth.entity.User;
import org.playground.booking.auth.provider.PGBookingUserDetails;
import org.playground.booking.security.auth.UserService;
import org.playground.booking.security.dao.ICustomerDao;
import org.playground.booking.security.dao.IMerchantDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private ICustomerDao customerDao;
    @Autowired
    private IMerchantDao merchantDao;


    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        User user = customerDao.loadCustomerByName(userName);
        if(user==null){
            user = merchantDao.loadMerchantByName(userName);
        }

        if(user!=null){
            PGBookingUserDetails userDetails = new PGBookingUserDetails(user);
            return userDetails;
        }else{
            throw new UsernameNotFoundException(userName);
        }
    }
}
